package com.compass.doctor.ui.patient.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.R
import com.compass.doctor.base.BaseVBFragment
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.databinding.FragmentInspectionReportBinding
import com.compass.doctor.ui.WebDetailActivity
import com.compass.doctor.ui.patient.adapter.InspectionReportAdapter
import com.compass.doctor.ui.patient.viewmodel.PatientViewModel
import com.compass.framework.constant.BASE_PDF
import com.compass.framework.constant.KEY_ORDER_TYPE
import com.compass.framework.constant.KEY_PATIENT_ID
import com.compass.framework.ext.gone
import com.compass.framework.ext.onClick
import com.compass.framework.ext.visible
import com.compass.framework.manager.UserInfoManager
import com.compass.framework.model.InspectionReport
import com.compass.framework.utils.DateUtils
import com.compass.framework.utils.getColorFromResource
import com.compass.framework.utils.getDrawableResource
import java.net.URLDecoder

/**
 * Description： 报告单fragment
 * Date:2024/3/26 9:40
 */
class InspectionReportFragment : BaseVBFragment<FragmentInspectionReportBinding>() {

    private val patientViewModel: PatientViewModel by viewModels()
    private lateinit var mAdapter: InspectionReportAdapter
    private lateinit var patientId: String //患者id
    private lateinit var reportType: String //类型
    private var type: Int = 0 //0-门诊 1-住院 2-体检
    override fun initView(view: View, savedInstanceState: Bundle?) {
        arguments?.let {
            patientId = it.getString(KEY_PATIENT_ID)!!
            reportType = it.getString(KEY_ORDER_TYPE)!!
        }
        initRecyclerView()
    }

    private var onCustomButtonClick = object : OnCustomButtonClick {
        override fun onButtonCallBack(position: Int, msgType: String?) {
            val item = mAdapter.getItem(position)
            item?.let {
                openFilm(it)
            }
        }
    }




    private fun initRecyclerView() {

        mAdapter = InspectionReportAdapter(itemType())
        mBinding?.recyclerView?.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
        }
        mAdapter.onItemClickListener = { _, position ->
            val item = mAdapter.getItem(position)
            item?.let {
                when(reportType){
                    "LIS"->{
                        openPDF(it.pdfUrl)
                    }

                    "PACS"->{
                        if (isShowCloudFilm(it.reportName)){
                            openFilm(it)
                        }else{
                            openPDF(it.pdfUrl)
                        }

                    }
                }

            }
        }
        mAdapter.setOnCustomItemClick(onCustomButtonClick)

    }

    private fun openPDF(pdfUrl:String){
        WebDetailActivity.start(
            requireContext(),
            BASE_PDF + URLDecoder.decode(pdfUrl, "utf-8"),
            "报告单"
        )
    }

    private fun openFilm(item: InspectionReport){
        UserInfoManager.getUserInfo()?.appHospitalConfigRespVO?.inspectionPacs?.let { basUrl ->
            var url =
                "${basUrl}?type=0&patientid=${item.patientId}&acessionno=${item.reportNumber}"
            WebDetailActivity.start(requireContext(), url, "云胶片")
        }
    }

    private fun isShowCloudFilm(reportName: String?): Boolean {
        reportName?.let {
            return (it.contains("数字化摄影（DR）") || it.contains("CT") || it.contains(
                "1.5T MR"
            )
                    || it.contains("3.0T MR") || it.contains("数字胃肠") || it.contains(
                "乳腺X线摄影"
            ))
        }?:kotlin.run {
            return false
        }

    }


    override fun intListener() {
        mBinding?.apply {
            tvOutpatientService.onClick {
                type = 0
                tvOutpatientService.setTextColor(getColorFromResource(R.color.white))
                tvBeHospitalized.setTextColor(getColorFromResource(R.color.color_2e344a))
                tvExamination.setTextColor(getColorFromResource(R.color.color_2e344a))
                tvOutpatientService.background = getDrawableResource(R.drawable.btn_shape_blue_50)
                tvBeHospitalized.background = getDrawableResource(R.drawable.bg_shape_line_50)
                tvExamination.background = getDrawableResource(R.drawable.bg_shape_line_50)
                getPatientInspectionList()
            }
            tvBeHospitalized.onClick {
                type = 1
                tvBeHospitalized.setTextColor(getColorFromResource(R.color.white))
                tvOutpatientService.setTextColor(getColorFromResource(R.color.color_2e344a))
                tvExamination.setTextColor(getColorFromResource(R.color.color_2e344a))
                tvOutpatientService.background = getDrawableResource(R.drawable.bg_shape_line_50)
                tvBeHospitalized.background = getDrawableResource(R.drawable.btn_shape_blue_50)
                tvExamination.background = getDrawableResource(R.drawable.bg_shape_line_50)
                getPatientInspectionList()
            }
            tvExamination.onClick {
                type = 2
                tvExamination.setTextColor(getColorFromResource(R.color.white))
                tvBeHospitalized.setTextColor(getColorFromResource(R.color.color_2e344a))
                tvOutpatientService.setTextColor(getColorFromResource(R.color.color_2e344a))
                tvOutpatientService.background = getDrawableResource(R.drawable.bg_shape_line_50)
                tvBeHospitalized.background = getDrawableResource(R.drawable.bg_shape_line_50)
                tvExamination.background = getDrawableResource(R.drawable.btn_shape_blue_50)
                getPatientInspectionList()
            }

        }
    }

    override fun initData() {
        getPatientInspectionList()
        patientViewModel.reportLiveData.observe(this) {
            dismissLoading()
            mAdapter.setData(it)
            if (it.isNullOrEmpty()) {
                mBinding?.emptyView?.visible()
            } else {
                mBinding?.emptyView?.gone()
            }

        }
    }

    private fun isNuclear(): Boolean {
        return (reportType == "NUCLEAR")
    }

    private fun itemType(): Int {
        return if (isNuclear()) {
            4
        } else {
            1
        }
    }
    private fun getPatientInspectionList() {
        showLoading()
//        var patientId="1787726757288951810" 测试患者
        when(reportType){
            "LIS"->{
                patientViewModel.getInspectionReport(patientId, reportType, type,null,
                    DateUtils.TimeAndDate(DateUtils.getToday()))
            }
            "PACS"->{
                patientViewModel.getInspectionReport(patientId, reportType, type,"2018-01-01 00:00:00",
                    DateUtils.TimeAndDate(DateUtils.getToday()))
            }
            "NUCLEAR"->{
                patientViewModel.getInspectionReport(patientId, reportType, type,"2018-01-01 00:00:00",
                    null)
            }
            else->{
                patientViewModel.getInspectionReport(patientId, reportType, type,null,
                    null)
            }
        }



    }


}